#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"../include/main.h"
#include"../include/hash.h"
#include"../include/mount_vfs.h"
#include"../include/narrtree.h"
#include"../include/vfs_errorcodes.h"
FILE *fp;
unsigned int hash(char name[])
{	
        
	llist *item1;
	int a;
	int c = *name;//equivalent to filename[0]
	a=c-32;
        /*if(c > 96) {//For Uppercase characters
	a = c - 32;
	return a % TABSIZ;
	}
	else
	return a % TABSIZ;*/
	//printf("ascii value=%d\n",a);
	return a;
}

void searchfile1(char name[],char outfile[])
{	
	int a,c,flag=0,i,count=0;
	int num = strlen(name);
        
	fp = fopen(outfile,"w");
	if(fp == NULL)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_SEARCHFILE_01);
	else
	{
		llist *item1;
		c = *name;
		a = c - 32;
		item1 = htable[a];
		while (item1!=NULL && item1->desc->filetype==0)
		{
			count=0;
			for(i=0;i<num;i++) 
			{
				if (name[i] == item1->desc->filename[i])
				count++;
				else
				break;
			}
			if (count == num)
			{
				fprintf(fp,"%s\n",item1->desc->filepath);
				flag=1;
			}
			item1 = item1->next;
		}
		//if(flag==1)
		printf("searchfile_SUCCESS\n");
	}
	
	if(fp != NULL)
	fclose(fp);
}


void listfile1(char name[],char outfile[])
{
	llist *item;
	ntree *tofind;
	char t[MAX_PATH],*pch;
	int i=0,j=0;
	schflag=0;
	tofind=search_node(head,name);
	schflag=0;
	if(tofind==NULL)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_LISTFILE_01);
	else
	{
		
		fp = fopen(outfile,"w");
		if(fp == NULL)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_LISTFILE_03);
		else
		{
			pch=strrchr(name,'/');
			pch++;
			if((item = htable[hash(pch)]) != NULL)
			{
				for (item = htable[hash(pch)]; item; item = item->next) 
				{
					if (strcmp(name, item->desc->filepath) == 0) 
					{
						i = item->desc->blk_num;
						fprintf(fp,"%s\n",data[i]);
					} 
				}
			}
			printf("listfile_SUCCESS\n");
		}
		
		if(fp != NULL)
		fclose(fp);
	}
}

llist *find(fdesc *itemdesc)
{
	llist *item;
	if((item = htable[hash(itemdesc->filename)]) != NULL)
	{
		for (item = htable[hash(itemdesc->filename)]; item; item = item->next)
       		if (!strcmp(itemdesc->filepath, item->desc->filepath))
            		return item;
	}
	return NULL;
}


llist *insert(fdesc *itemdesc)
{
	llist *item,*item1;
	int h;
	if ((item1 = find(itemdesc)) == NULL){
    	if ((item = (llist *)malloc(sizeof (llist))) != NULL)
	{
		item->desc=(fdesc *)malloc(sizeof(fdesc));
		strcpy(item->desc->filename,itemdesc->filename);
		strcpy(item->desc->filepath,itemdesc->filepath);
		item->desc->filetype=itemdesc->filetype;
		item->desc->blk_num=itemdesc->blk_num;
		item->desc->file_size=itemdesc->file_size;
		h = hash(itemdesc->filename);
		item->next = htable[h];
		htable[h] = item;
	}
	}
	else
		return NULL;
	return item;
}


int deletion(fdesc *itemdesc)
{
	llist *temp,*prev;
	int h = hash(itemdesc->filename); 
	temp=htable[h];
	while(temp!=NULL)
	{
		if(!strcmp(temp->desc->filepath,itemdesc->filepath))
		{
			if(temp==htable[h])
			{
				htable[h]=temp->next;
				free(temp);
				return 1;
			}
			else
			{
				prev->next=temp->next;
				free(temp);
				return 1;
			}
		}
		else
		{
			prev=temp;
			temp= temp->next;
		}
	}
	return 0;
}

void printTable()
{
	int i=0;
	llist *temp;
	for(i=0;i<=TABSIZ;i++)
	{
		temp=htable[i];
		while(temp!=NULL)
		{
			printf("\n%d -  %s\n", i, temp->desc->filepath);
			temp=temp->next;
		}
	}
}
